Public Class frmOutputs

    Private m_aobjBuildedCases As CaseItem()
    Private m_objMyOutputs As MyOutputs
    Private m_blnFillingList As Boolean

    Public Sub New(ByVal outputs As MyOutputs)
        ' This call is required by the Windows Form Designer.
        Me.InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.        
        Me.cboCaseList.Tag = -1
        m_aobjBuildedCases = frmMain.BuildedCases
        For Each objCaseItem As CaseItem In m_aobjBuildedCases
            Me.cboCaseList.Items.Add(objCaseItem.Title)
            If outputs Is objCaseItem.Data.Outputs Then Me.cboCaseList.Tag = Me.cboCaseList.Items.Count - 1
        Next
        Me.cboCaseList.SelectedIndex = CInt(Me.cboCaseList.Tag)
        Me.Translate()
    End Sub


    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.lvwSelected.Items.Clear()
        Me.FillFileList()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim objMyGraphics As New MyGraphics

        For Each objListViewGroup As ListViewGroup In Me.lvwSelected.Groups
            Dim blnMultiCase As Boolean = False
            Dim strLastCase As String = Nothing
            Dim objGraphic As MyGraphics.Graphic = Nothing

            For Each objListViewItem As ListViewItem In objListViewGroup.Items
                If strLastCase IsNot Nothing AndAlso strLastCase <> objListViewItem.SubItems(1).Text Then
                    blnMultiCase = True
                    Exit For
                End If
                strLastCase = objListViewItem.SubItems(1).Text
            Next

            For Each objListViewItem As ListViewItem In objListViewGroup.Items
                For Each objCaseItem As CaseItem In m_aobjBuildedCases
                    If objCaseItem.Title = objListViewItem.SubItems(1).Text Then
                        Dim strTitle As String = ""
                        Dim strLabel As String = ""
                        Dim strAxisLabel As String

                        If objGraphic Is Nothing Then
                            If objListViewGroup.Items.Count = 1 Then
                                strTitle = objListViewItem.Text + " : " + objCaseItem.Title
                            ElseIf Not blnMultiCase Then
                                strTitle = objCaseItem.Title
                            End If
                            If Me.cboTypeList.SelectedIndex = 2 Then
                                strAxisLabel = Language.ReadMessages("msgVOLTS")
                            Else
                                strAxisLabel = Language.ReadMessages("msgMICRON")
                            End If
                            objGraphic = objMyGraphics.Add(strTitle, strAxisLabel, objListViewGroup.Name)
                        End If

                        If blnMultiCase Then
                            strLabel = objListViewItem.Tag.ToString + " (" + objCaseItem.Title + ")"
                        Else
                            strLabel = objListViewItem.Text
                        End If

                        For Each objOutputData As MyOutputs.OutputData In objCaseItem.Data.Outputs.DataList(objListViewItem.Tag.ToString)
                            objGraphic.AddLine(strLabel, objOutputData.Xdata, objOutputData.Ydata)
                        Next
                    End If
                Next
            Next
        Next

        Me.Hide() : My.Application.DoEvents()

        If objMyGraphics.Count > 0 Then
            With New frmGraphics(objMyGraphics)
                .ShowDialog()
            End With
        End If
    End Sub

    Private Sub cboCaseList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCaseList.SelectedIndexChanged
        m_objMyOutputs = m_aobjBuildedCases(Me.cboCaseList.SelectedIndex).Data.Outputs
        If Me.cboTypeList.Items.Count = 0 Then Me.FillTypeList()
        Me.FillFileList()
    End Sub

    Private Sub cboGroupList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboGroupList.SelectedIndexChanged
        Me.cboVoltageList.Items.Clear()
        Me.cboVoltageList.Items.AddRange(m_objMyOutputs.OptionList(MyOutputs.OptionConstants.Voltage, Me.cboTypeList.SelectedIndex, Me.cboGroupList.SelectedIndex).ToArray)
        If Me.cboVoltageList.Items.Count > 0 Then
            Me.cboVoltageList.SelectedIndex = 0
        Else
            Me.FillFileList()
        End If
    End Sub

    Private Sub cboTypeList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboTypeList.SelectedIndexChanged
        My.Application.DoEvents()
        Me.cboGroupList.Items.Clear()
        Me.cboGroupList.Items.AddRange(m_objMyOutputs.OptionList(MyOutputs.OptionConstants.Group, Me.cboTypeList.SelectedIndex).ToArray)
        If Me.cboGroupList.Items.Count > 0 Then
            Me.cboGroupList.SelectedIndex = 0
        Else
            Me.cboVoltageList.Items.Clear()
            Me.FillFileList()
        End If
    End Sub

    Private Sub cboVoltageList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboVoltageList.SelectedIndexChanged
        My.Application.DoEvents()
        Me.FillFileList()
    End Sub

    Private Sub lvwFiles_ItemChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles lvwFiles.ItemChecked
        If Not m_blnFillingList Then
            Dim strFile As String = e.Item.SubItems.Item(1).Text

            If e.Item.Checked Then
                Dim astrItem As String() = e.Item.Text.Split(New String() {"["}, StringSplitOptions.RemoveEmptyEntries)
                Dim objGroup As ListViewGroup = Nothing
                Dim strName As String = astrItem(0).TrimEnd
                Dim strGroup As String = astrItem(1).Replace("]", "")

                For Each objListViewGroup As ListViewGroup In Me.lvwSelected.Groups
                    If objListViewGroup.Name = strGroup Then
                        objGroup = objListViewGroup
                        Exit For
                    End If
                Next
                If objGroup Is Nothing Then objGroup = Me.lvwSelected.Groups.Add(strGroup, strGroup)
                With Me.lvwSelected.Items.Add(strName + " (" + strFile + ")")
                    .SubItems.Add(Me.cboCaseList.Text)
                    .Tag = strFile
                    .Group = objGroup
                End With
                Me.lvwSelected.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
            Else
                For Each objListViewItem As ListViewItem In Me.lvwSelected.Items
                    If objListViewItem.Tag.ToString = strFile And objListViewItem.SubItems.Item(1).Text = Me.cboCaseList.Text Then
                        objListViewItem.Remove()
                        Exit For
                    End If
                Next
            End If
        End If
    End Sub


    Private Sub FillFileList()
        m_blnFillingList = True
        Me.lvwFiles.Items.Clear()
        For Each objOuputData As MyOutputs.OutputFile In m_objMyOutputs.FileList(Me.cboTypeList.SelectedIndex, Me.cboGroupList.SelectedIndex, CDbl(IIf(Me.cboVoltageList.Text.Length > 0, Me.cboVoltageList.Text, "0")))
            With Me.lvwFiles.Items.Add(objOuputData.Title)
                .SubItems.Add(objOuputData.Name)
                For Each objListViewItem As ListViewItem In Me.lvwSelected.Items
                    If objListViewItem.Tag.ToString = objOuputData.Name And objListViewItem.SubItems.Item(1).Text = Me.cboCaseList.Text Then
                        .Checked = True
                        Exit For
                    End If
                Next
            End With
        Next
        If Me.lvwFiles.Items.Count > 0 Then Me.lvwFiles.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
        m_blnFillingList = False
    End Sub

    Private Sub FillTypeList()
        Me.cboTypeList.Items.Clear()
        Me.cboTypeList.Items.AddRange(m_objMyOutputs.OptionList(MyOutputs.OptionConstants.Type).ToArray)
        Me.cboTypeList.SelectedIndex = 0
    End Sub

    Private Sub Translate()
        Me.lblCase.Text = Language.ReadOutputs("lblCase")
        Me.lblType.Text = Language.ReadOutputs("lblType")
        Me.lblGroup.Text = Language.ReadOutputs("lblGroup")
        Me.lblVoltage.Text = Language.ReadOutputs("lblVoltage")        
        Me.pagFiles.Text = Language.ReadOutputs("pagFiles")
        Me.pagSelected.Text = Language.ReadOutputs("pagSelected")
        Me.lvwFiles.Columns(0).Text = Language.ReadOutputs("colTitle")
        Me.lvwFiles.Columns(1).Text = Language.ReadOutputs("colFile")
        Me.lvwSelected.Columns(0).Text = Language.ReadOutputs("colTitle")
        Me.lvwSelected.Columns(1).Text = Language.ReadOutputs("colCase")
        Me.btnClear.Text = Language.ReadOutputs("btnClear")
        Me.btnOK.Text = Language.ReadMessages("msgOK")
        Me.btnCancel.Text = Language.ReadMessages("msgCANCEL")
    End Sub

End Class